/**
 * Created by Brook on 2019/2/19 0019.
 */
(function(){



    const model_config={
        host: '127.0.0.1',
        //host: '121.40.31.195',
        port:3306,
        user: 'root',
        password: '123456',
        database: 'model_conversion',
        connectionLimit: 20
    }



    //Sql
    const SQL_TXT = {
        /**
         * conversionModel
         */
        //插入数据
        insert_model_info: "REPLACE INTO `model_conversion`.`model_lists` (`uuid`, `fileid`, `filepath`,`projectid`, `notifyurl`, `suffix`, `filename`, `modelname`, `projectname`,`gltfstate`,`data_date`, `createtime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        delete_model_info:"DELETE FROM `model_conversion`.`model_lists` WHERE fileid=?",
        up_model_info:"UPDATE model_conversion.model_lists SET `md5`='',appid='',parsingstate='0',parsingcode='',parsingmessage='队列中，等待解析' WHERE fileid=?;",

        /**
         * getModel
         */
        //查询数据 - 解析
        parsing_get_data:"SELECT `id`,`uuid`,`fileId`,`filePath`,`projectId`,`modelName`,`projectName`,`notifyUrl` FROM model_lists WHERE suffix='skp' AND parsingstate='0' AND fileid!=0 ORDER BY createtime ASC limit ?;",
        check_task:"SELECT `id`,`uuid`,`fileId`,`filePath`,`projectId`,`modelName`,`projectName`,`notifyUrl` FROM model_lists WHERE suffix='skp' AND parsingstate='1' AND appid=? AND fileid!=0 ORDER BY createtime ASC limit ?;",
        //修改数据 - 解析状态
        parsing_up_data:"UPDATE model_conversion.model_lists SET `parsingstate`='1',appid=? WHERE id in (?);",
        //查询数据 - 转换表
        get_statue_list:"SELECT * FROM `model_conversion`.`model_lists` WHERE ",
        //查询数据 - 参数表
        get_statue_info:"SELECT listid,modelid,filename,modelname FROM `model_conversion`.`model_infos` WHERE ",
        //查询数据 - 整合参数表
        get_integration_info:"SELECT * FROM `model_conversion`.`model_infos` WHERE listid in (?);",
        //查询数据 - 整合参数表
        get_integration_list:"SELECT * FROM `model_conversion`.`model_lists` WHERE id in (?);",
        //查询数据
        get_modellist:"SELECT id FROM `model_conversion`.`model_lists` WHERE uuid = ?;",
        get_modelinfo:"SELECT * FROM `model_conversion`.`model_infos` WHERE listid = ?;",
        //写入数据
        insert_modelinfo:"REPLACE INTO `model_conversion`.`model_infos` (`listid`,`modelid`,`filename`,`modelname`,`data_date`,`createtime`) VALUES (?);",
        //更新数据
        update_modelinfo:"UPDATE `model_conversion`.`model_infos` SET ",
        //MD5判断模型是否解析
        get_checkmd5:"SELECT uuid FROM `model_conversion`.`model_lists` WHERE md5 = ?;",
        //创建uuid
        create_uuid:"REPLACE INTO `model_conversion`.`model_lists` (`uuid`, `fileid`, `filepath`,`projectid`, `notifyurl`, `suffix`, `filename`, `appid`, `parsingstate`, `data_date`, `createtime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        //修改数据 - 解析状态 选择模型解析
        choice_parsing_up_data:"UPDATE model_conversion.model_lists SET `parsingstate`='1',appid=? WHERE uuid=?;",
        //查询数据 - 解析
        choice_parsing_get_data:"SELECT `id`,`uuid`,`fileId`,`filePath`,`projectId`,`modelName`,`projectName`,`notifyUrl` FROM model_lists WHERE uuid=?;",

        /**
         * clientInfo
         */
        //修改状态 - 客户端信息
        up_clieninfo:"UPDATE `model_conversion`.`model_clientinfos` SET `dataState`=? WHERE appId=?;",  //已废弃
        del_clieninfo:"DELETE FROM `model_conversion`.`model_clientinfos`WHERE appId=?;",
        //写入数据 - 客户端信息
        insert_clieninfo:"REPLACE INTO `model_conversion`.`model_clientinfos` (`appId`,`appPath`,`appVersion`,`isPackaged`,`localIp`,`port`,`public_ip`,`rvtToPbcExePath`,`saveMajorJsonFilePath`,`serverFileDownloadCover`,`serverOpened`,`serverSKPRVTFileSavePath`,`skpSupportVersion`,`skpToPbcExePath`,`hasQueue`,`dataState`,`platform`,`openedUI`,`data_date`,`createtime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
        //查询数据 - 客户端信息
        get_clieninfo:"SELECT * FROM `model_conversion`.`model_clientinfos` WHERE ",


        /**
         * stateModel
         */
        //查询数据 - 获取fileid
        get_fileId_uuid:"SELECT id,parsingstate,fileid,notifyurl FROM `model_conversion`.`model_lists` WHERE uuid=?",
        get_fileId:"SELECT parsingstate FROM `model_conversion`.`model_lists` WHERE fileid=?",
        up_state:"UPDATE `model_conversion`.`model_lists` SET `parsingstate`=?, `parsingcode`=?, `parsingmessage`=? WHERE uuid=?;",
        get_modelId:"SELECT * FROM `model_conversion`.`model_infos` WHERE listid=?",
        attribute_up_md5:"UPDATE `model_conversion`.`model_lists` SET `md5`=? WHERE uuid=?;",
        attribute_up_modelId:"UPDATE `model_conversion`.`model_infos` SET `modelid`=? WHERE listid=?;",
        insert_logs:"REPLACE INTO `model_conversion`.`model_logs` (`uuid`,`fileid`,`parsingstate`,`parsingcode`,`parsingmessage`,`data_date`,`createtime`) VALUES (?);",

        /**
         * stateModel
         */
        //查询数据 - 查询状态
        get_state:"SELECT `fileid`,`parsingstate`,`parsingcode`,`parsingmessage` FROM `model_conversion`.`model_lists` WHERE fileid=?",
        //查询数据 - 取消任务
        get_task:"SELECT id FROM `model_conversion`.`model_lists` WHERE uuid=?",
        delete_list:"DELETE FROM `model_conversion`.`model_lists` WHERE uuid=?",
        delete_info:"DELETE FROM `model_conversion`.`model_infos` WHERE listid=?",

        //附加属性
        get_attributes:"SELECT id FROM `model_conversion`.`model_attributes` WHERE listid = ?;",
        insert_attributes:"REPLACE INTO `model_conversion`.`model_attributes` (`listid`,`data_date`,`createtime`) VALUES (?);",
        update_attributes:"UPDATE `model_conversion`.`model_attributes` SET ",



        /**
         * formatServer
         */
        //查询数据 - 解析
        format_get_data:"SELECT `id`,`uuid`,`fileId`,`filePath`,`projectId`,`modelName`,`projectName`,`notifyUrl`,`gltfstate` FROM model_lists WHERE (suffix not in ('skp','rvt') AND formatstate='0') OR (gltfpath='' AND gltfstate='1') ORDER BY createtime ASC limit 1;",
        format_check_task:"SELECT `id`,`uuid`,`fileId`,`filePath`,`projectId`,`modelName`,`projectName`,`notifyUrl`,`gltfstate` FROM model_lists WHERE (suffix not in ('skp','rvt') AND formatstate='1') OR (gltfpath='' AND gltfstate='2') ORDER BY createtime ASC limit 1;",
        //修改数据 - 解析状态
        format_parsing_up_data:"UPDATE model_conversion.model_lists SET `formatstate`=?,gltfstate=? WHERE id=?;",

        //格式转换完成
        get_format_task:"SELECT * FROM model_lists WHERE uuid=?;",
        up_gltfurl:"UPDATE `model_conversion`.`model_lists` SET `gltfpath`=? WHERE uuid=?;",
        insert_format_info: "REPLACE INTO `model_conversion`.`model_lists` (`uuid`, `fileid`, `filepath`,`projectid`, `notifyurl`, `suffix`, `filename`, `modelname`, `projectname`,`gltfstate`,`data_date`, `createtime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",

        format_up_state:"UPDATE `model_conversion`.`model_lists` SET `formatstate`=?, `parsingcode`=?, `parsingmessage`=? ,gltfstate=? WHERE uuid=?;",

        //获取revit解析任务
        revittask_get_data:"SELECT `id`,`uuid`,`fileId`,`filePath`,`projectId`,`modelName`,`projectName`,`notifyUrl`,`gltfstate` FROM model_lists WHERE suffix='rvt' AND parsingstate='0' ORDER BY createtime ASC limit 1;",
        revittask_check_task:"SELECT `id`,`uuid`,`fileId`,`filePath`,`projectId`,`modelName`,`projectName`,`notifyUrl`,`gltfstate` FROM model_lists WHERE suffix='rvt' AND parsingstate='1' ORDER BY createtime ASC limit 1;",
        revittask_up_data:"UPDATE model_conversion.model_lists SET `formatstate`=? WHERE id=?;",


        /**
         * Gltf 预处理
         */
        check_gltf_data:"SELECT `id`,`uuid`,`fileId`,`filePath`,`gltfpath`,`projectId`,`modelName`,`projectName`,`notifyUrl` FROM model_lists WHERE gltfstate='5' AND appid=?;",
        get_gltf_data_task:"SELECT `id`,`uuid`,`fileId`,`filePath`,`gltfpath`,`projectId`,`modelName`,`projectName`,`notifyUrl` FROM model_lists WHERE gltfstate='3' ORDER BY createtime ASC limit 1;",
        up_gltf_data_task:"UPDATE model_conversion.model_lists SET `gltfstate`='5',appid=? WHERE id=?;",

        check_gltf_data_uuid:"SELECT id FROM model_lists WHERE uuid=?;",
        up_gltf_data_shvpath:"UPDATE model_conversion.model_lists SET `gltfstate`='7',shvpath=? WHERE uuid=?;",

        up_gltf_state:"UPDATE `model_conversion`.`model_lists` SET `gltfstate`=?, `parsingcode`=?, `parsingmessage`=? WHERE uuid=?;",

        get_gltf_data:"SELECT `uuid`,`fileId`,`gltfpath`,`shvpath`,`modelName`,`projectName`,`gltfstate` FROM model_lists WHERE gltfstate!='0';",


        /**
         * 渲染平台 - 模型转换Gltf
         */
        insert_xrmodel_info: "REPLACE INTO `model_conversion`.`model_gltf_lists` (`uuid`,`filepath`,`suffix`,`filename`,`mac`,`gltfstate`,`data_date`,`createtime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
        check_gltf_data_mac:"SELECT `shvpath`,`filename`,`mac`,`gltfstate` FROM model_gltf_lists WHERE mac=?;",
        get_xrgltf_data_task:"SELECT `uuid`,`filepath` FROM model_gltf_lists WHERE gltfstate='1' AND suffix !='rvt' ORDER BY createtime ASC limit 1;",
        get_xrrvtgltf_data_task:"SELECT `uuid`,`filepath` FROM model_gltf_lists WHERE gltfstate='1' AND suffix ='rvt' ORDER BY createtime ASC limit 1;",
        up_xrgltf_data_task:"UPDATE model_conversion.model_gltf_lists SET `gltfstate`=?,parsingcode=?,parsingmessage=? WHERE uuid=?;",
        check_xrgltf_data_uuid:"SELECT id,gltfpath,binpath,shvpath FROM model_gltf_lists WHERE uuid=?;",
        up_xrgltf_data_shvpath:"UPDATE model_conversion.model_gltf_lists SET gltfpath=?,binpath=?,shvpath=? WHERE uuid=?;",

    };


    const SQL_TXT_API = {
        //列表信息
        get_list:"SELECT * FROM `model_conversion`.`model_lists` ORDER BY createtime ASC;",
        get_list_condition:"SELECT * FROM `model_conversion`.`model_lists` WHERE ",

        //modelID
        get_modelId_list:"SELECT listid,modelid FROM `model_conversion`.`model_infos` WHERE listid in (?)",
        get_modelId:"SELECT * FROM `model_conversion`.`model_infos` WHERE modelid=?",

        //stateLog
        get_stateLog:"SELECT * FROM `model_conversion`.`model_logs` WHERE uuid=?",

        //clientInfo
        get_clientInfo:"SELECT * FROM `model_conversion`.`model_clientinfos` WHERE appId=?",

        //clientInfo
        get_list_id:"SELECT id,createtime FROM `model_conversion`.`model_lists` WHERE uuid=?;",
        get_attributes:"SELECT * FROM `model_conversion`.`model_attributes` WHERE listid=?;",

        //upDataName
        upDataName:"UPDATE `model_conversion`.`model_lists` SET `modelname`=?, `projectname`=? WHERE uuid=?;",

        //adjustOrder
        upadjustOrder:"UPDATE `model_conversion`.`model_lists` c JOIN 	`model_conversion`.`model_lists` cc ON (c.uuid = ? AND cc.uuid = ?) OR (c.uuid = ? AND cc.uuid = ?) SET	c.createtime = cc.createtime , cc.createtime = c.createtime;",

        //againSubmit
        upList:"UPDATE `model_conversion`.`model_lists` SET `md5`='', `appid`=NULL, `parsingstate`='0', `parsingcode`='', `parsingmessage`='队列中，等待解析' WHERE uuid=?;",
        delLogs:"DELETE FROM `model_conversion`.`model_logs` WHERE uuid=?;",
        delAttributes:"DELETE FROM `model_conversion`.`model_attributes` WHERE listid=?;",
        delInfo:"DELETE FROM `model_conversion`.`model_infos` WHERE listid=?;",

        //delList
        delList:"DELETE FROM `model_conversion`.`model_lists` WHERE uuid=?;",

        //getqueuenum
        get_listNum:"SELECT COUNT(1) AS COUNT FROM `model_conversion`.`model_lists` WHERE parsingstate='0' AND createtime<?;",

        //getGltflist
        get_Gltflist:"SELECT `uuid`,`modelname`,`gltfpath`,`gltfstate` FROM `model_conversion`.`model_lists` WHERE gltfstate != '0' ORDER BY createtime ASC;",

    };

    module.exports = {
        Model_Config:model_config,
        SQL_TXT:SQL_TXT,
        SQL_TXT_API:SQL_TXT_API,

    };

}).call(this);
